package com.csun.cmny.provider.controller;


import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.ServiceOrderComplaintConstant;
import com.csun.cmny.provider.model.domain.OmcServiceComplaint;
import com.csun.cmny.provider.model.domain.OmcServiceOrder;
import com.csun.cmny.provider.model.dto.ServiceComplaintDto;
import com.csun.cmny.provider.model.dto.ServiceOrderQueryDto;
import com.csun.cmny.provider.model.dto.comolaint.ServiceComplaintSaveDto;
import com.csun.cmny.provider.model.dto.comolaint.ServiceComplaintSelectDto;
import com.csun.cmny.provider.model.vo.complaint.ServiceComplaintVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcServiceComplaintService;
import com.csun.cmny.provider.service.OmcServiceOrderService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/serviceComplaint", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcServiceComplaintController extends BaseController {

    @Resource
    private OmcServiceComplaintService complaintService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcServiceOrderService orderService;



    /*
     * 提交投诉
     * */
    @PostMapping(value = "/submit")
    public Wrapper submit (@RequestBody ServiceComplaintSaveDto serviceComplaintSaveDto) {




        OmcServiceOrder omcServiceOrder = orderService.selectByKey(serviceComplaintSaveDto.getServiceOrderId());

        //校验订单是否存在
        if (PublicUtil.isEmpty(omcServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.order.error", null));
        }

        //校验是否本人订单
        if (!omcServiceOrder.getUserIdPut().equals(getLoginAuthDto().getUserId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.complaint.notMy", null));
        }

        //校验此订单是否已经投诉过
        if (omcServiceOrder.getComplaintStatus()!= ServiceOrderComplaintConstant.ORDER_COMPLAINT_NULL){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.complaint.save.error", null));
        }


        int result=complaintService.submit(serviceComplaintSaveDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*
     * 查看投诉
     * */
    @PostMapping(value = "/select")
    public Wrapper select (@RequestBody ServiceComplaintSelectDto serviceComplaintSelectDto) {


        List<ServiceComplaintVo> serviceComplaintVos=complaintService.selectComplaint(serviceComplaintSelectDto);


        PageInfo<ServiceComplaintVo> pageInfo = new PageInfo<>(serviceComplaintVos);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /*
     * 服务人员查看投诉
     * */
    @PostMapping(value = "/listComplaint")
    public Wrapper listComplaint(@RequestBody ServiceComplaintDto serviceComplaintDto){
        serviceComplaintDto.setUserId(getLoginAuthDto().getUserId());
        PageInfo pageInfo=complaintService.listComplaint(serviceComplaintDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success",null),pageInfo);
    }

    /*
     * 处理投诉
     * */
    @PostMapping(value = "/dispose")
    public Wrapper dispose (Long serviceOrderId) {

        //订单是否存在
        OmcServiceOrder omcServiceOrder = orderService.selectByKey(serviceOrderId);
        if (PublicUtil.isEmpty(omcServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("complaint.serviceOrder.isnull", null));
        }

        //此投诉不存在
       if (omcServiceOrder.getComplaintStatus()==ServiceOrderComplaintConstant.ORDER_COMPLAINT_NULL){
           return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                   .getMessage("complaint.is.null", null));
       }
       //此投诉已经处理
       if (omcServiceOrder.getComplaintStatus()==ServiceOrderComplaintConstant.ORDER_COMPLAINT_RECEIVE){
           return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                   .getMessage("complaint.dispose.error", null));
       }

       omcServiceOrder.setComplaintStatus(ServiceOrderComplaintConstant.ORDER_COMPLAINT_RECEIVE);

        int result = orderService.update(omcServiceOrder);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }
}
